package TestSystemUtil;

import java.util.*;

public class Protocol {
	public static final int WAITING = 0;
	public static final int REGISTER = 1;
	public static final int LOGIN = 2;
	public static final int SINGLEPLAY = 3;
	public static final int GROUPPLAY = 4;
	public static final int PLAY = 5;
	public static final int LOGOUT = 6;
	public static final int INVITE = 7;
	public static final int ACCEPT = 8;
	public static final int DECLINE = 9;
	public static final int LEAVEGROUP = 10;
	public static final int EXIT = 11;
	public static final int GET_HIGHSCORE = 12;
	public static final int GROUPSUBMIT= 13;
	public static final int SINGLESUBMIT = 14;
	public static final int GET_HISTORY = 15;
	public int state = WAITING;
	
	public int getState() {
		return state;
	}
	public void setState(int state) {
		this.state = state;
	}
	public String getMessageForm() {
		String sqlMessage = null;
		switch(state) {
		case WAITING:
			return null;
		case LOGIN:
			sqlMessage = "LOGIN+%s+%s";
			break;
		case REGISTER:
			sqlMessage = "REGISTER+%s+%s+%s+%s+%s+%s";
			break;
		case PLAY: 
			sqlMessage = "PLAY+%s+%s";
			break;
		case GROUPPLAY:
			sqlMessage = "GROUPPLAY";
			break;
		case LOGOUT:
			sqlMessage = "LOGOUT";
			break;
		case EXIT:
			sqlMessage = "EXIT";
			break;
		case INVITE:
			sqlMessage = "INVITE+%s";
			break;
		case ACCEPT:
			sqlMessage = "ACCEPT+%s";
			break;
		case DECLINE:
			sqlMessage = "DECLINE+%s";
			break;
		case LEAVEGROUP:
			sqlMessage = "LEAVEGROUP";
			break;
		case GROUPSUBMIT:
			sqlMessage = "GROUPSUBMIT+%s+%s";
			break;
		case GET_HIGHSCORE:
			sqlMessage = "GET_HIGHSCORE";
			break;
		case SINGLESUBMIT: 
			sqlMessage = "SINGLESUBMIT+%s+%s+%s";
			break;
		case GET_HISTORY:
			sqlMessage  ="GET_HISTORY+%s";
			break;
		}
		return sqlMessage;
	}

	public ArrayList<String> analyseMessageFromServer (String message) {
		StringTokenizer tokenizer = new StringTokenizer(message, "+");
		ArrayList<String> result = new ArrayList<String>();
	
		switch(tokenizer.nextToken()) {
		case "SUCCESS": 
			result.add("SUCCESS");
			break;
		case "ERR":
			result.add("ERR");
			break;
		case "REGISTER":
			result.add("REGISTER");
			break;
		case "ONLINE":
			result.add("ONLINE");
			break;
		case "OFFLINE":
			result.add("OFFLINE");
			break;
		case "INVITE":
			result.add("INVITE");
			break;
		case "ACCEPT":
			result.add("ACCEPT");
			break;
		case "DECLINE":
			result.add("DECLINE");
			break;
		case "GROUPSTART":
			result.add("GROUPSTART");
			break;
		case "GROUPQUESTION":
			result.add("GROUPQUESTION");
			break;
		case "GROUPRESULT":
			result.add("GROUPRESULT");
			break;
		case "GROUPINFO":
			result.add("GROUPINFO");
			break;
		case "HIGHSCORE":
			result.add("HIGHSCORE");
			break;
		case "SINGLESUBMIT":
			result.add("SINGLESUBMIT");
			break;
		case "HISTORY":
			result.add("HISTORY");
			break;
		}
		while(tokenizer.hasMoreTokens()) result.add(tokenizer.nextToken());
		return result;
	}
}
